<!-- 绘制DQG -->
<template>
  <div></div>
</template>

<script>
import { drawDQG, cancelDrawDQG } from "@/dqg/DrawDQG";
import { drawOneAirLine } from '@/dqg/drawOneAirLine';


export default {
  name: "DrawDQG",
  data() {
    return {
      drawDQGOptions: {
        // 是否显示网格信息（双击触发）
        isShowGridInfo: false,
        closeGridInfo: false,
        pickedGrid: undefined,
      },
      url: [
        // "test.json",
        "Variflight_BJN1659_20210328.json",

        "Variflight_ABW698_20210327.json",
        "Variflight_ABW698_20210328.json",
        "Variflight_BJN1586_20210328.json",
        "Variflight_BJN1659_20210328.json",
        "Variflight_CAO1082_20210327.json",
        "Variflight_CC1781_20210327.json",
        "Variflight_CCA1109_20210328.json",
        "Variflight_CCA1146_20210328.json",
        "Variflight_CCA1230_20210328.json",
        "Variflight_CCA1258_20210328.json",
        "Variflight_CCA1310_20210328.json",
        "Variflight_CCA1350_20210328.json",
        "Variflight_CCA1386_20210327.json",
        "Variflight_CCA1465_20210328.json",
        "Variflight_CCA1540_20210328.json",
        "Variflight_CCA1617_20210328.json",
        "Variflight_CCA1638_20210328.json",
        "Variflight_CCA1662_20210328.json",
        "Variflight_CCA1701_20210327.json",
        "Variflight_CCA1781_20210327.json",
        "Variflight_CCA1802_20210327.json",
        "Variflight_CCA1820_20210328.json",
        "Variflight_CCA1822_20210327.json",
        "Variflight_CCA1846_20210328.json",
        "Variflight_CCA1947_20210327.json",
        "Variflight_CCA1961_20210327.json",
        "Variflight_CCA1979_20210327.json",
        "Variflight_CCA4069_20210328.json",
        "Variflight_CCA4070_20210328.json",
        "Variflight_CCA4129_20210328.json",
        "Variflight_CCA4133_20210328.json",
        "Variflight_CCA4161_20210327.json",
        "Variflight_CCA4164_20210327.json",
        "Variflight_CCA4164_20210328.json",
        "Variflight_CCA4171_20210327.json",
        "Variflight_CCA4184_20210326.json",
        "Variflight_CCA4193_20210327.json",
        "Variflight_CCA4550_20210327.json",
        "Variflight_CCA4593_20210327.json",
        "Variflight_CCA4594_20210327.json",
        "Variflight_CCA611_20210325.json",
        "Variflight_CCA8201_20210328.json",
        "Variflight_CCD1606_20210327.json",
        "Variflight_CCD8903_20210328.json",
        "Variflight_CDC8789_20210327.json",
        "Variflight_CDG4651_20210327.json",
        "Variflight_CDG4851_20210327.json",
        "Variflight_CDG4878_20210327.json",
        "Variflight_CES5099_20210327.json",
        "Variflight_CES5101_20210328.json",
        "Variflight_CES5102_20210326.json",
        "Variflight_CES5141_20210327.json",
        "Variflight_CES5191_20210328.json",
        "Variflight_CES5192_20210328.json",
        "Variflight_CES5203_20210328.json",
        "Variflight_CES5247_20210327.json",
        "Variflight_CES5269_20210327.json",
        "Variflight_CES5423_20210328.json",
        "Variflight_CES5499_20210327.json",
        "Variflight_CES5505_20210328.json",
        "Variflight_CES5637_20210327.json",
        "Variflight_CES5765_20210327.json",
        "Variflight_CES5766_20210328.json",
        "Variflight_CES6327_20210328.json",
        "Variflight_CES6447_20210328.json",
        "Variflight_CES6471_20210327.json",
        "Variflight_CES7553_20210327.json",
        "Variflight_CES771_20210327.json",
        "Variflight_CES779_20210325.json",
        "Variflight_CES7901_20210327.json",
        "Variflight_CES9059_20210327.json",
        "Variflight_CES9059_20210328.json",
        "Variflight_CES9060_20210328.json",
        "Variflight_CES9676_20210328.json",
        "Variflight_CES9718_20210328.json",
        "Variflight_CES9966_20210327.json",
        "Variflight_CHH439_20210327.json",
        "Variflight_CHH701_20210325.json",
        "Variflight_CHH7181_20210328.json",
        "Variflight_CHH7346_20210328.json",
        "Variflight_CHH7378_20210327.json",
        "Variflight_CHH7431_20210327.json",
        "Variflight_CHH7710_20210327.json",
        "Variflight_CHH8330_20210327.json",
        "Variflight_CKK257_20210326.json",
        "Variflight_CKK263_20210326.json",
        "Variflight_CNM1142_20210328.json",
        "Variflight_CNM1237_20210327.json",
        "Variflight_CNM1238_20210328.json",
        "Variflight_CNM8104_20210327.json",
        "Variflight_CPA3372_20210328.json",
        "Variflight_CQH6221_20210327.json",
        "Variflight_CQH6265_20210327.json",
        "Variflight_CQH6345_20210325.json",
        "Variflight_CQH6445_20210325.json",
        "Variflight_CQH6495_20210328.json",
        "Variflight_CQH8731_20210328.json",
        "Variflight_CQH8779_20210327.json",
        "Variflight_CQH8809_20210327.json",
        "Variflight_CQH8819_20210324.json"
      ],
      level: 11
    };
  },
  mounted() {
    // 管理网格绘制过程中的状态
    this.$viewer.drawDQGState = {};

    /** 监听组件的点击事件 */
    this.$EventBus.$on("drawDefault", () => {
      this.drawDefault();
    });

    this.$EventBus.$on("draw2dDQG", () => {
      this.draw2dDQG();
    });

    this.$EventBus.$on("drawMultiDQG", () => {
      this.drawMultiDQG();
    });

    this.$EventBus.$on("factorChange", (factor) => {
      this.factorChange(factor);
    });

    this.$EventBus.$on("doubleClick", (event) => {
      this.doubleClick(event);
    });

    this.$EventBus.$on("closeGridInfo", (event) => {
      this.closeGridInfo(event);
    });

    this.$EventBus.$on("drawAirLine", () => {
      this.drawAirLine();
    });
  },
  computed: {},

  methods: {
    /**
     * 默认绘制二维单尺度DQG网格
     */
    drawDefault() {
      this.$store.commit("drawDefault");
      this.$store.commit("drawMultiDQGFalse");
      this.$store.commit("showSelectDrawTrue");
      this.drawDQGOptions.subdivFactor = 5;

      if (this.$store.state.isDrawDefault && !this.$store.state.isDraw2dDQG) {
        cancelDrawDQG(this.$viewer.scene);
        this.drawDQGOptions.DQGStyle = 1;
        drawDQG(this.$viewer, this.drawDQGOptions);
      } else if (!this.$store.state.isDraw2dDQG) {
        cancelDrawDQG(this.$viewer.scene);
      } else if (
        !this.$store.state.isDrawDefault &&
        this.$store.state.isDraw2dDQG
      ) {
        cancelDrawDQG(this.$viewer.scene);
        this.$store.commit("draw2dDQGFalse");
        this.$store.commit("showSelectDrawFalse");
      }
    },

    /**
     * 绘制二维单尺度DQG网格
     */
    draw2dDQG() {
      this.$store.commit("draw2dDQG");
      this.$store.commit("drawMultiDQGFalse");
      this.$store.commit("showSelectDrawTrue");
      this.curDQGIndex = 0;
      // 默认情况下细分阈值为5
      this.drawDQGOptions.subdivFactor = 5;

      if (this.$store.state.isDraw2dDQG && !this.$store.state.isDrawDefault) {
        cancelDrawDQG(this.$viewer.scene);
        this.drawDQGOptions.DQGStyle = 1;
        drawDQG(this.$viewer, this.drawDQGOptions);
      } else if (!this.$store.state.isDrawDefault) {
        cancelDrawDQG(this.$viewer.scene);
      } else if (
        !this.$store.state.isDraw2dDQG &&
        this.$store.state.isDrawDefault
      ) {
        cancelDrawDQG(this.$viewer.scene);
        this.$store.commit("drawDefaultFalse");
        this.$store.commit("showSelectDrawFalse");
      }
    },

    /**
     * 绘制二维多尺度网格
     */
    drawMultiDQG() {
      this.$store.commit("drawMultiDQG");
      this.$store.commit("drawDefaultFalse");
      this.$store.commit("draw2dDQGFalse");
      this.$store.commit("showSelectDrawTrue");
      cancelDrawDQG(this.$viewer.scene);

      this.curDQGIndex = 1;
      this.drawDQGOptions.subdivFactor = 5;

      if (this.$store.state.isDrawMultiDQG) {
        this.drawDQGOptions.DQGStyle = 2;
        drawDQG(this.$viewer, this.drawDQGOptions);
      } else {
        cancelDrawDQG(this.$viewer.scene);
        this.$store.commit("showSelectDrawFalse");
      }
    },

    /**
     * 细分阈值发生变化
     */
    factorChange(factor) {
      this.drawDQGOptions.subdivFactor = factor;
      if (this.$store.state.isDraw2dDQG) {
        cancelDrawDQG(this.$viewer.scene);
        drawDQG(this.$viewer, this.drawDQGOptions);
      } else {
        drawDQG(this.$viewer, this.drawDQGOptions);
      }
    },

    /**
     * 屏幕双击时，显示网格信息
     */
    doubleClick(event) {
      this.$store.commit("showGridInfoTrue");
      // 确保了触发后只绘制一次，不会每帧都画
      this.drawDQGOptions.isDrawSurface = !this.drawDQGOptions.isDrawSurface;
      this.$viewer.drawDQGState.isShowGridInfo =
        this.drawDQGOptions.isShowGridInfo;
      this.drawDQGOptions.screenUV = new Cesium.Cartesian2(
        event.offsetX,
        event.offsetY
      );
    },

    /**
     * 关闭网格信息框
     */
    closeGridInfo() {
      this.drawDQGOptions.closeGridInfo = true;
    },

    /**
     * 绘制航线
     */
    drawAirLine() {
      // ColorGeometryInstanceAttribute 每个实例几何图形颜色的值和类型信息
      const cameraInfo = new Cesium.ColorGeometryInstanceAttribute(
        0.8,
        0.5,
        0.2,
        0.4
      );
      // 遍历航线数据
      let self = this.$viewer;
      for (let i = 0; i < 1; i++) {
        let currentUrl = "/airline_data/" + this.url[i];
        drawOneAirLine(self, this.level, currentUrl, cameraInfo);
      }
    }
  },
  watch: {
    // 监听鼠标双击网格事件
    "drawDQGOptions.pickedGrid": {
      handler: function (newValue) {
        if (newValue) {
          this.$EventBus.$emit("pickedGridInfo", newValue);
        }
      },
    },
    // 主要监听在DrawDQG.js里双击空白处时清空网格信息框
    "drawDQGOptions.isShowGridInfo": {
      handler: function (newValue) {
        if (!newValue) {
          this.$store.commit("showGridInfoFalse");
        }
      },
    },
  },
};
</script>

<style scoped>
</style>